import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

//引入login组件
import Login from "@@/login/login.vue";
import Home from "@@/home/home.vue";

import Welcome from "../views/welcome/welcome.vue";
import NotFound from "../views/notFound/notFound.vue";

// import User from '../views/user/user.vue'

// 路由懒加载
const User = () => import("../views/user/user.vue");
const Roles = () => import("../views/roles/roles.vue");
const Rights = () => import("../views/rights/rights.vue");
const Categories = () => import("../views/categories/categories.vue");
const Params = () => import("../views/params/params.vue");
const Reports = () => import("../views/reports/reports.vue");
const Goods = () => import("../views/goods/goods.vue");
const Orders = () => import("../views/orders/orders.vue");

const router = new VueRouter({
  mode: "history",
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login
    },
    { path: "*", component: NotFound },
    {
      path: "/",
      name: "home",
      component: Home,
      redirect: {
        path: "welcome"
      },
      children: [
        {
          name: "welcome",
          path: "welcome",
          component: Welcome,
          meta: { name: "welcome" }
        },
        {
          name: "users",
          path: "users",
          component: User,
          meta: { name: "users" }
        },
        {
          name: "roles",
          path: "roles",
          component: Roles
        },
        {
          name: "rights",
          path: "rights",
          component: Rights
        },
        {
          name: "categories",
          path: "categories",
          component: Categories
        },
        {
          name: "params",
          path: "params",
          component: Params
        },
        {
          name: "reports",
          path: "reports",
          component: Reports
        },
        {
          name: "goods",
          path: "goods",
          component: Goods
        },
        {
          name: "orders",
          path: "orders",
          component: Orders
        }
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  //1 获取token 从localStorage

  let isToken = window.localStorage.getItem("tokenLogin");
  if (isToken) {
    next();
  } else {
    if (to.path === "/login") {
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
